package com.youi.util;

import com.youi.entity.Merchantcert;
import com.youi.model.FileInfo;
import com.youi.third.yibaopay.YibaopayUtil;
import org.apache.commons.lang.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.MalformedURLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;


/**
 * 对象工具类
 *
 * @param <T>
 * @author erdong.Chan
 */
public class ObjectUtil {

    /**
     * 判断对象所有String属性是否为null，如果是设置为""字符串
     *
     * @param obj
     */
    public static void ToChangeNullToEmpty(Object obj) {
        Class cls = obj.getClass();
        Field[] fields = cls.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String name = field.getName();
            if (field.getType().equals(String.class)) {    //如果为字符串类型
                String upperName = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());//获取大写开头变量名
                try {
                    if (cls.getDeclaredMethod("get" + upperName).invoke(obj) == null) {    //判断是否为空  如果为空设值为""
                        cls.getDeclaredMethod("set" + upperName, String.class).invoke(obj, "");
                    }
                } catch (IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException | NoSuchMethodException
                        | SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 判断对象所有String属性是否为null
     *
     * @param obj
     */
    public static boolean checkAllPropertyIsEmpty(Object obj) {
        Class cls = obj.getClass();
        Field[] fields = cls.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String name = field.getName();
            if (field.getType().equals(String.class)) {    //如果为字符串类型
                String upperName = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());//获取大写开头变量名
                try {
                    if (cls.getDeclaredMethod("get" + upperName).invoke(obj) != null && !cls.getDeclaredMethod("get" + upperName).invoke(obj).equals("")) {    //判断不等于空 且不等于""
                        return false;
                    }
                } catch (IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException | NoSuchMethodException
                        | SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    /**
     * list去重复
     **/
    public static List removeDuplicate(List arlList) {
        HashSet h = new HashSet(arlList);
        arlList.clear();
        arlList.addAll(h);
        return arlList;
    }

    /**
     * list转string逗号隔开
     *
     * @param strList
     * @return
     */
    public static String listToString(List<Long> strList) {
        if (strList != null && strList.size() > 0) {
            String strs = StringUtils.strip(strList.toString(), "[]");
            String result = strs.replace(" ", "");
            return result;
        }
        return null;
    }

    /**
     * 精度计算 返回整形
     *
     * @param v1
     * @param v2
     * @return
     */
    public static Integer Intgermul(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2).intValue();
    }

    public static String FormatFloat(Float flo) {
        DecimalFormat decimalFormat = new DecimalFormat(".00");
        return decimalFormat.format(flo);
    }

    public static String FormatDouble(Double dou) {
        DecimalFormat decimalFormat = new DecimalFormat(".00");
        return decimalFormat.format(dou);
    }

    /**
     * 两数相乘
     *
     * @param v1
     * @param v2
     * @return
     */
    public static Integer mul(Integer v1, Float v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.multiply(b2).intValue();
    }

    /**
     * 两数相乘
     *
     * @param v1
     * @param v2
     * @return
     */
    public static Float mulGetFloat(Integer v1, Float v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.multiply(b2).floatValue();
    }

    public static String mulGetString(Integer v1, Float v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.multiply(b2).floatValue() + "";
    }

    /**
     * 相除
     */
    public static Integer divide(Integer v1, Integer v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.divide(b2, 2, RoundingMode.HALF_UP).intValue();
    }

    /**
     * 相除
     */
    public static Float divide(Float v1, Integer v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.divide(b2, 2, RoundingMode.HALF_UP).floatValue();
    }

    /**
     * 根据ids字符串获取list
     *
     * @param ids
     * @return
     */
    public static List<Long> getIdListByIds(String ids) {
        List<Long> idsList = null;
        if (ids != null && ids.length() > 0) {
            String[] idsArr = ids.split(",");
            if (idsArr != null && idsArr.length > 0) {
                idsList = new ArrayList<Long>();
                for (String id : idsArr) {
                    idsList.add(Long.valueOf(id));
                }
            }
        }
        return idsList;
    }

    /**
     * 获取性别描述
     *
     * @param gender
     * @return
     */
    public static String getGenderDesc(Integer gender) {
        if (gender != null) {
            switch ((int) gender) {
                case 0:
                    return "女";
                case 1:
                    return "男";
            }
        }
        return "未知";
    }

    /**
     * 获取用户身份
     * 商户(0否 1普通商家 2兼职商家)
     *
     * @return
     */
    public static String getBusiness(Integer business) {
        if (business != null) {
            switch ((int) business) {
                case 0:
                    return "否";
                case 1:
                    return "普通商家";
                case 2:
                    return "兼职商家";
            }
        }
        return "未知";
    }

    /**
     * 获取性别描述
     *
     * @param gender
     * @return
     */
    public static String getCheckStaus(Integer status) {
        if (status != null) {
            switch ((int) status) {
                case -1:
                    return "不通过";
                case 0:
                    return "待审核";
                case 1:
                    return "已通过";
            }
        }
        return "";
    }

    /**
     * @param state 订单状态(-1已关闭 0待付款 1待发货 2待收货 3待评价 4已完成 5售后)
     * @return
     */
    public static String getOrderState(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case -1:
                    return "已关闭";
                case 0:
                    return "待付款";
                case 1:
                    return "待发货";
                case 2:
                    return "待收货";
                case 3:
                    return "待评价";
                case 4:
                    return "已完成";
                case 5:
                    return "售后";
            }
        }
        return "";
    }

    public static String getRefundOrderState(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case -2:
                    return "退款失败";
                case -1:
                    return "未收货";
                case 0:
                    return "退款中";
                case 1:
                    return "退款成功";
            }
        }
        return "";
    }

    public static String getRefundOrderApplyState(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case -1:
                    return "不通过";
                case 0:
                    return "未审核";
                case 1:
                    return "通过";
                case 2:
                    return "关闭订单";
            }
        }
        return "";
    }

    public static String getRefundOrderTakeState(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case -1:
                    return "未收货";
                case 0:
                    return "已收货";
                case 1:
                    return "不退货";
            }
        }
        return "";
    }

    /**
     * 精度计算 返回double类型
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double Doublemul(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2).doubleValue();
    }

    /**
     * 获取提现状态(-1 未处理 0已处理)
     *
     * @param state
     * @return
     */
    public static String getWithdrawalState(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case -1:
                    return "未支付";
                case 0:
                    return "已处理";
            }
        }
        return "";
    }

    /**
     * 获取佳选官提现状态()
     *
     * @param state
     * @return
     */
    public static String getJXGWithdrawalState(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case -1:
                    return "作废";
                case 0:
                    return "未审核";
                case 1:
                    return "已审核（提现中）";
                case 2:
                    return "提现成功";
                case 3:
                    return "提现失败";

            }
        }
        return "";
    }

    /**
     * 获取计价单位
     *
     * @param gender
     * @return
     */
    public static String getPriceUnit(Integer priceType) {
        if (priceType != null) {
            switch ((int) priceType) {
                case 0:
                    return "小时";
                case 1:
                    return "天";
            }
        }
        return "未知";
    }

    /**
     * 获取服务状态
     *
     * @param gender
     * @return
     */
    public static String getServicesState(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case 0:
                    return "已下架";
                case 1:
                    return "在售";
            }
        }
        return "未知";
    }

    /**
     * 根据string字符串获取list
     *
     * @param ids
     * @return
     */
    public static List<String> getStringListByIds(String strs) {
        List<String> strsList = null;
        if (strs != null && strs.length() > 0) {
            String[] strArr = strs.split(",");
            if (strArr != null && strArr.length > 0) {
                strsList = new ArrayList<String>();
                for (String str : strArr) {
                    strsList.add(str);
                }
            }
        }
        return strsList;
    }

    /**
     * 获取支付方式
     *
     * @param gender
     * @return
     */
    public static String getPayType(Integer payType) {
        if (payType != null) {
            switch ((int) payType) {
                case -1:
                    return "未支付";
                case 0:
                    return "余额支付";
                case 1:
                    return "银行卡支付";
                case 2:
                    return "微信支付";
                case 3:
                    return "支付宝支付";
            }
        }
        return "未知";
    }

    /**
     * 获取是否
     *
     * @param gender
     * @return
     */
    public static String getIf(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case 0:
                    return "否";
                case 1:
                    return "是";
            }
        }
        return "未知";
    }

    /**
     * 补齐数字
     *
     * @param count
     * @return
     */
    public static String getCode(String str) {
        String code = null;
        switch (str.length()) {
            case 1:
                code = "00000" + str;
                break;
            case 2:
                code = "0000" + str;
                break;
            case 3:
                code = "000" + str;
                break;
            case 4:
                code = "00" + str;
                break;
            case 5:
                code = "0" + str;
                break;
            case 6:
                code = str;
                break;
            default:
                code = "999999";
                break;
        }
        return code;
    }

    /**
     * 去除字符串中所包含的空格
     *
     * @param s
     * @return
     */
    public static String removeAllBlank(String s) {
        String result = "";
        if (null != s && !"".equals(s)) {
            result = s.replaceAll(" ", "");
        }
        return result;
    }

    /**
     * 获取供应商提现状态(0不可提现 1未提现 2提现中 3提现成功 4提现失败)
     *
     * @param merchantStatus
     * @return
     */
    public static String getMerchantStatus(Integer merchantStatus) {
        if (merchantStatus != null) {
            switch (merchantStatus) {
                case 0:
                    return "不可提现";
                case 1:
                    return "未提现 ";
                case 2:
                    return "提现中";
                case 3:
                    return "提现成功";
                case 4:
                    return "提现失败";
            }
        }
        return "未知";
    }

    /**
     * 获取供应商审核状态(-1作废 0未审核 1已通过)
     *
     * @param merchantStatus
     * @return
     */
    public static String getMerchantApplyStatus(Integer merchantStatus) {
        if (merchantStatus != null) {
            switch (merchantStatus) {
                case -1:
                    return "作废";
                case 0:
                    return "未审核 ";
                case 1:
                    return "已通过";
            }
        }
        return "未知";
    }

    /**
     * 获取提现申请审核转态
     * 状态(-1作废 0未审核 1已审核(待打款) 2已审核(延迟打款) 3提现成功 4提现失败 5延迟打款)
     *
     * @param merchantStatus
     * @return
     */
    public static String getApplyStatus(Integer merchantStatus) {
        if (merchantStatus != null) {
            switch (merchantStatus) {
                case -1:
                    return "作废";
                case 0:
                    return "未审核 ";
                case 1:
                    return "已审核(待打款)";
                case 2:
                    return "已审核(延迟打款)";
                case 3:
                    return "提现成功";
                case 4:
                    return "提现失败";
                case 5:
                    return "延迟打款";
            }
        }
        return "未知";
    }

    /**
     * 获取平台提现申请审核状态
     * 审核状态(-1作废 0未审核 1已审核(待打款 ) 2提现成功 3提现失败)
     *
     * @param merchantStatus
     * @return
     */
    public static String getPlatformApplyStatus(Integer merchantStatus) {
        if (merchantStatus != null) {
            switch (merchantStatus) {
                case -1:
                    return "作废";
                case 0:
                    return "未审核 ";
                case 1:
                    return "已审核(待打款)";
                case 2:
                    return "提现成功";
                case 3:
                    return "提现失败";
            }
        }
        return "未知";
    }

    /**
     * 获取平台提现申请审核状态
     * 审核状态(0不可提现 1未提现 2提现中 3提现成功 4提现失败)
     *
     * @param merchantStatus
     * @return
     */
    public static String getPlatformApplyStatusNew(Integer platformStatus) {
        if (platformStatus != null) {
            switch (platformStatus) {
                case 0:
                    return "不可提现";
                case 1:
                    return "未提现";
                case 2:
                    return "提现中";
                case 3:
                    return "提现成功";
                case 4:
                    return "提现失败";
            }
        }
        return "未知";
    }

    /**
     * 获取打款类型
     *
     * @param type 0供应商打款 1平台打款 2团长打款
     * @return
     */
    public static String getTremittanceType(Integer type) {
        if (type != null) {
            switch (type) {
                case 0:
                    return "供应商打款 ";
                case 1:
                    return "平台打款";
                case 2:
                    return "团长打款";
            }
        }
        return "未知";
    }

    /**
     * 获取打款申请审核状态
     *
     * @param status -1不同意打款 0未审核 1同意打款 2延迟打款
     * @return
     */
    public static String getTremittanceStatus(Integer status) {
        if (status != null) {
            switch (status) {
                case -1:
                    return "不同意打款 ";
                case 0:
                    return "未审核";
                case 1:
                    return "同意打款";
                case 2:
                    return "延迟打款";
            }
        }
        return "未知";
    }

    /**
     * 获取佣金流水类型
     *
     * @param status 变更类型(0佣金订单收入 1提现支出 2退款支出)
     * @return
     */
    public static String getLeaderprofitType(Integer status) {
        if (status != null) {
            switch (status) {
                case 0:
                    return "佣金订单收入";
                case 1:
                    return "提现支出";
                case 2:
                    return "退款支出";
            }
        }
        return "未知";
    }

    /**
     * 获取商家证件资质集合
     *
     * @param merchantcert
     * @return
     * @throws IOException
     * @throws MalformedURLException
     */
    public static List<FileInfo> getFileInfo(Merchantcert merchantcert) throws MalformedURLException, IOException {
        List<FileInfo> fileInfoList = new ArrayList<FileInfo>();
        String quaUrl = YibaopayUtil.yibaoUploadFile(merchantcert.getIdCardFront());
        fileInfoList.add(new FileInfo("IDCARD_FRONT", quaUrl));
        quaUrl = YibaopayUtil.yibaoUploadFile(merchantcert.getIdCareBehind());
        fileInfoList.add(new FileInfo("IDCARD_BACK", quaUrl));
        quaUrl = YibaopayUtil.yibaoUploadFile(merchantcert.getUsicCert());
        fileInfoList.add(new FileInfo("UNI_CREDIT_CODE", quaUrl));
        quaUrl = YibaopayUtil.yibaoUploadFile(merchantcert.getOpenaccountCert());
        fileInfoList.add(new FileInfo("OP_BANK_CODE", quaUrl));
        quaUrl = YibaopayUtil.yibaoUploadFile(merchantcert.getHandInCard());
        fileInfoList.add(new FileInfo("HAND_IDCARD", quaUrl));
        return fileInfoList;
    }

    /**
     * 获取订单id
     *
     * @param type 订单类型
     * @param id
     * @return
     */
    public static String getOrderNo(Integer type, String id) {
        if (type != null && id != null && id.trim().length() > 0) {
            String orderNo = type.toString() + getCode(id) + new Date().getTime();
            return orderNo;
        }
        return "5999999" + new Date().getTime();
    }

    /**
     * 获取商品状态
     *
     * @param status 状态( -1已拒绝 0待审核 1出售中 2已售罄 3已下架  4未出售 5已结束)
     * @return
     */
    public static String getGoodsStaus(Integer status) {
        if (status != null) {
            switch (status) {
                case -1:
                    return "已拒绝";
                case 1:
                    return "出售中";
                case 2:
                    return "已售罄";
                case 3:
                    return "已下架";
                case 4:
                    return "未出售";
                case 5:
                    return "已结束";
            }
        }
        return "未知";
    }

    /**
     * 获取商品审核状态
     *
     * @param status 状态( -1已拒绝 1已通过)
     * @return
     */
    public static String getGoodsAuditStaus(Integer status) {
        if (status != null) {
            switch (status) {
                case -1:
                    return "已拒绝";
                case 1:
                    return "已通过";
            }
        }
        return "未知";
    }

    /**
     * 获取分润状态
     *
     * @param profitStatus 分润状态(-1分账失败 0未分账 1已分账)
     * @return
     */
    public static String getProfitStatus(Integer profitStatus) {
        if (profitStatus != null) {
            switch (profitStatus) {
                case -1:
                    return "分账失败";
                case 0:
                    return "未分账";
                case 1:
                    return "已分账";
            }
        }
        return "未知";
    }

    /**
     * @param value 需要科学计算的数据
     * @param digit 保留的小数位
     * @return 功能：四舍六入五成双计算法
     */
    public static Double sciCal(double value, int digit) {
        String result = "-999";
        try {
            double ratio = Math.pow(10, digit);
            double _num = value * ratio;
            double mod = _num % 1;
            double integer = Math.floor(_num);
            double returnNum;
            if (mod > 0.5) {
                returnNum = (integer + 1) / ratio;
            } else if (mod < 0.5) {
                returnNum = integer / ratio;
            } else {
                returnNum = (integer % 2 == 0 ? integer : integer + 1) / ratio;
            }
            BigDecimal bg = new BigDecimal(returnNum);
            result = bg.setScale((int) digit, BigDecimal.ROUND_HALF_UP).toString();
        } catch (RuntimeException e) {
            throw e;
        }
        return Double.valueOf(result);
    }

    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1) {
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }

    public static void main(String[] args) throws NoSuchMethodException, SecurityException {

    }
}
